import { fileURLToPath, URL } from 'node:url'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    port: 5173, // 前端项目运行端口
    proxy: {
      // 配置代理规则，这里以 "/api" 为前缀举例
      '/api/product': {
        target: 'http://localhost:6004', // 目标接口域名
        changeOrigin: true, // 允许跨域
        rewrite: (path) => path.replace(/^\/api\/product/, ''), // 移除请求路径中的 "/api/product/" 前缀
        // 如果是https接口，需要配置这个参数
        secure: false
      },
      '/api/user': {
        target: 'http://localhost:6002', // 目标端口2
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/user/, ''), // 移除 "/api/user/" 前缀
        secure: false
      },
      '/api/order': {
        target: 'http://localhost:6003', // 目标端口2
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/order/, ''), // 移除 "/api/order/" 前缀
        secure: false
      },
      '/api/permission': {
        target: 'http://localhost:6006', // 目标端口3
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/permission/, ''), // 移除 "/api/permission/" 前缀
        secure: false
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 自动导入定制化样式文件进行样式覆盖
        additionalData: `
          @use "@/styles/var.scss" as *;
        `,
      }
    }
  }
})
